<?php 
defined('IN_TS') or die('Access Denied.');

//只允许学习，不允许退学

switch($ts){

	//开始学习课程
	case "do":
		
		$userid = intval($TS_USER['userid']);
		if($userid == 0){
			echo 0;exit;//请登陆
		}
		
		$studyid = intval($_POST['studyid']);
		
		$isLearn = $new['study']->findCount('study_learn',array(
			'userid'=>$userid,
			'studyid'=>$studyid,
		));
		
		if($isLearn > 0){
			echo 1;exit;//你已经学习了该课程
		}
		
		$strStudy = $new['study']->find('study',array(
			'studyid'=>$studyid,
		));
		

		//启动支付帐号
		$strUserPay = aac('pay')->getUserPay($userid);
		if($strUserPay['over']<$strStudy['price']){
			echo 2;exit;//支付帐号资金不足，请充值后再进行购买学习
		}
		
		
		//付款消费
		aac('pay')->updatePay($userid,$strStudy['price'],1,'购买学习课程'.$strStudy['studyid']);
		
		
		$new['study']->create('study_learn',array(
			'userid'=>$userid,
			'studyid'=>$studyid,
			'status'=>1,
			'addtime'=>time(),
		));
		
		
		
		//统计学习人数
		$count_learn = $new['study']->findCount('study_learn',array(
			'studyid'=>$studyid,
		));
		
		$new['study']->update('study',array(
			'studyid'=>$studyid,
		),array(
			'count_learn'=>$count_learn,
		));
		
		echo 3;exit;//学习加付费成功
		
		break;

}